<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="公司" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <!-- 一级部门pid为'' -->
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加一级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="data"
          :props="defaultProps"
          default-expand-all
          @node-click="handleNodeClick"
        >
          <template slot-scope="{ node, data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="公司" /><span>{{ node.label }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 添加或者修改 -->
    <el-dialog
      :title="isEdit ? '编辑部门' : '添加部门'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="hDialogClose"
    >
      <dept-dialog
        ref="deptDialog"
        :pid="curId"
        :is-edit="isEdit"
        :vali-list="originList"
        @success="hSuccess"
        @close="hClose"
      />
      <!-- 方法：v-if="showDialog" -->
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import deptDialog from './deptDialog.vue'

export default {
  components: { deptDialog },
  data() {
    return {
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 弹框开关
      showDialog: false,
      // 子组件需要的数据
      curId: '',
      // 判断是否点击的编辑
      isEdit: false,
      // 校验数据
      originList: []
    }
  },
  created() {
    // 获取员工信息
    this.loadDepartments()
  },
  methods: {
    handleNodeClick(data) {
      console.log(data)
    },
    // 获取员工信息
    async loadDepartments() {
      const res = await getDepartments()
      // console.log(res)
      res.data.depts.shift()
      // 把平铺的数组结构转成树形结构
      this.data = tranListToTreeData(res.data.depts)
      // 校验数据
      this.originList = res.data.depts.map((item) => {
        return {
          id: item.id,
          pid: item.pid,
          code: item.code,
          name: item.name
        }
      })
    },
    // 添加子部门
    hAdd(id) {
      // 开启弹框
      this.showDialog = true
      // console.log(id)
      this.curId = id
      // 判断点击
      this.isEdit = false
    },
    // 收到子组件中的success事件
    hSuccess() {
      // 关闭弹框
      this.showDialog = false
      // 重新获取数据
      this.loadDepartments()
    },
    // 收到子组件中的close事件
    hClose() {
      // 关闭弹框
      this.showDialog = false
    },
    // 编辑部门
    hEdit(id) {
      // 开启弹框
      this.showDialog = true
      // 传数据
      this.curId = id
      // 判断的值为true
      this.isEdit = true
      this.$nextTick(() => {
        this.$refs.deptDialog.loadDetail()
      })
    },
    // 删除请求
    async doDel(id) {
      await delDepartment(id)
      this.$message.success('删除成功')
    },
    // 删除弹框
    hDel(id) {
      // 弹层询问，是否删除
      this.$confirm('你确定要离开吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          // 确认
          this.doDel(id)
        })
        .catch(() => {
          // 取消
        })
    },
    // 清空表单验证效果
    hDialogClose() {
      this.$refs.deptDialog.hCancel()
    }
  }
}
</script>
